<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
      <el-form-item label="合同编号" prop="sn">
        <el-input v-model="queryParams.sn" placeholder="请输入合同编号" clearable @keyup.enter="handleQuery" class="!w-240px" />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker v-model="queryParams.createTime" value-format="YYYY-MM-DD HH:mm:ss" type="daterange"
          start-placeholder="开始日期" end-placeholder="结束日期" :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px" />
      </el-form-item>
      <el-form-item label="签订对象" prop="userId">
        <el-input v-model="queryParams.userId" placeholder="请输入签订对象" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item label="商砼企业" prop="companyId">
        <el-input v-model="queryParams.companyId" placeholder="请输入商砼企业" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" /> 搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" /> 重置
        </el-button>
      </el-form-item>

      <div class="cont-li">
        <div style="margin-right: 15px; font-size: 13px;" :class="{ constng: curindex == i }" v-for="(v, i) in userlist"
          :key="i" @click="btncont(i)">{{
            v
          }}</div>
      </div>

    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>

    <div class="card-header">
      <el-button type="primary">已签订</el-button>
      <el-button type="primary" plain @click="handleExport" :loading="exportLoading"
        v-hasPermi="['cms:art-category:export']">
        导出报告
      </el-button>
    </div>

    <el-table v-loading="loading" :data="list">
      <!--供货进度 
      <el-table-column label="签订对象" align="center" prop="userId" />
        <el-table-column label="项目id" align="center" prop="projectId" /> 
    <el-table-column label="用户签章" align="center" prop="userSign" />
        <el-table-column label="企业签章" align="center" prop="companySign" /> 
      <el-table-column label="合同内容" align="center" prop="body" /> -->
      <el-table-column type="selection" width="55" />
      <el-table-column label="合同编号" align="center" prop="sn" />
      <el-table-column label="供货进度" align="center" prop="schedule" />
      <el-table-column label="商砼企业" align="center" prop="companyId" />
      <el-table-column label="合同状态" align="center" prop="status" />
      <el-table-column label="签订时间" align="center" prop="createTime" />
      <el-table-column label="到期时间" align="center" prop="endTime" />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" @click="openForm('update', scope.row.id)">
            编辑
          </el-button>
        </template>
      </el-table-column>

    </el-table>
    <!-- 分页 -->
    <Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <ContractForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts" name="Contract">
// import { DICT_TYPE, getBoolDictOptions } from '@/utils/dict'
// import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import * as ContractApi from '@/api/cms/contract'
// import ContractForm from './ContractForm.vue'


const message = useMessage() // 消息弹窗
// const { t } = useI18n() // 国际化
const user = reactive({
  curindex: 1,
  userlist: ['集采委托协议', '水泥销购物合同-水泥商', '水泥购销合同-混凝土']
})

let { userlist } = toRefs(user)
let curindex = toRef(user, 'curindex')
const btncont = (i) => {
  curindex.value = i
}
const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  type: null,
  sn: null,
  userId: null,
  companyId: null,
  projectId: null,
  status: null,
  createTime: '',
  schedule: null
})

const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中


// 点击更换列表
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ContractApi.getContractPage({ queryParams })
    list.value = data.list
    total.value = data.total
    console.log(data, 1111)
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
// const handleDelete = async (id: number) => {
//   try {
//     // 删除的二次确认
//     await message.delConfirm()
//     // 发起删除
//     await ContractApi.deleteContract(id)
//     message.success(t('common.delSuccess'))
//     // 刷新列表
//     await getList()
//   } catch { }
// }

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await ContractApi.exportContract(queryParams)
    download.excel(data, '合同管理.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>


<style lang="scss" scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.cont-li {
  display: flex;
  font-size: 14px;
}

.constng {
  color: #429fff;
}

.el-card {
  border-radius: 15px;
}
</style>
